package it.polimi.traveldream.entity;

import it.polimi.traveldream.dto.CartPackageDTO;
import it.polimi.traveldream.dto.ScheduleDTO;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * The persistent class for the cartpackage database table.
 * 
 */
@Entity
@Table(name = "cartpackage")
@DiscriminatorValue("cart")
@NamedQuery(name = CartPackage.FIND_ALL, query = "SELECT c FROM CartPackage c WHERE c.user = :user")
public class CartPackage extends Package implements Serializable {
	private static final long serialVersionUID = 1L;

	public static final String FIND_ALL = "CartPackage.findAll";

	@NotNull
	@Temporal(TemporalType.DATE)
	private Calendar date;

	@NotNull
	private Boolean purchased;

	@NotNull
	@Min(1)
	private Integer quantity;

	@ManyToOne
	@JoinColumn(name = "model", nullable = false)
	private DefaultPackage model;

	@ManyToOne
	@JoinColumn(name = "user", nullable = true)
	private Customer user;

	public CartPackage() {
	}

	public CartPackage(CartPackageDTO packDTO) {
		this.setId(packDTO.getId());
		this.setCity(packDTO.getCity());
		this.setDuration(packDTO.getDuration());
		this.setPrice(packDTO.getPrice());
		this.setDate(packDTO.getDate());
		this.setQuantity(packDTO.getQuantity());
		this.setPurchased(packDTO.getPurchased());
		this.setModel(new DefaultPackage(packDTO.getModel()));
		this.setGoing(new Transport(packDTO.getGoing()));
		this.setBack(new Transport(packDTO.getBack()));
		this.setAccomodation(new Accomodation(packDTO.getAccomodation()));
		for (ScheduleDTO sched : packDTO.getSchedule().values()) {
			this.addSchedule(new Schedule(sched));
		}
	}

	private void setDate(Date date) {
		this.date = Calendar.getInstance();
		this.date.setTime(date);
	}

	public CartPackageDTO convertToDTO() {
		CartPackageDTO packDTO = new CartPackageDTO();
		packDTO.setId(this.getId());
		packDTO.setCity(this.getCity());
		packDTO.setDuration(this.getDuration());
		packDTO.setPrice(this.getPrice());
		packDTO.setDate(this.getDate());
		packDTO.setQuantity(this.getQuantity());
		packDTO.setPurchased(this.getPurchased());
		packDTO.setModel(this.getModel().convertToDetailedDTO());
		packDTO.setAccomodation(this.getAccomodation().convertToDTO());
		packDTO.setGoing(this.getGoing().convertToDTO());
		packDTO.setBack(this.getBack().convertToDTO());
		for (Schedule sched : this.getSchedule()) {
			packDTO.addSchedule(sched.convertToDTO());
		}
		return packDTO;
	}

	public Calendar getDate() {
		return this.date;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}

	public Boolean getPurchased() {
		return this.purchased;
	}

	public void setPurchased(Boolean purchased) {
		this.purchased = purchased;
	}

	public Integer getQuantity() {
		return this.quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public DefaultPackage getModel() {
		return this.model;
	}

	public void setModel(DefaultPackage model) {
		this.model = model;
	}

	public Customer getUser() {
		return this.user;
	}

	public void setUser(Customer user) {
		this.user = user;
	}

}